package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.come.Frame.ZhuFrame;
import org.come.bean.PhoneNumberSGBean;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.Util;
import org.come.until.sendsms;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.IphoneVerifyBtn;

/**
 * HGC<br>
 * 手机验证面板JPanel<br>
 * 窗体编号： 99
 * 
 * @author Administrator
 * 
 */
public class IphoneVerifyPanel extends JPanel {

	/**
	 * bindingBtn绑定菜单按钮1<br>
	 * bindingBtn解绑菜单按钮2<br>
	 * iphoneBtn绑定/解绑手机号按钮3<br>
	 * verificationBtn发送验证码按钮4<br>
	 */
	private IphoneVerifyBtn bindingBtn, unbundleBtn, iphoneBtn, verificationBtn;

	/**
	 * iphoneText手机号<br>
	 * verificationText验证码<br>
	 * safetyText安全码<br>
	 */
	private JTextField iphoneText, verificationText, safetyText;

	/** 存储验证码 */
	private String verification;
	/** 存储发送的手机号 */
	private String iphoneNumber;

	private PhoneNumberSGBean numberSGBean;// 存储系统发过来的手机号

	/**
	 * 判断当前按钮是否可以改变/ true不可改变
	 */
	private boolean btnchange = false;

	public IphoneVerifyPanel() {
		setPreferredSize(new Dimension(362, 308));
		setOpaque(false);
		setLayout(null);

		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 99);
		offBtn.setBounds(362 - 27, 2, 25, 25);
		this.add(offBtn);

		getBindingBtn();
		getUnbundleBtn();
		getIphoneText();
		getVerificationText();
		getSafetyText();
		getVerificationBtn();
		getIphoneBtn();

	}

	/**
	 * 1是绑定手机号<br>
	 * 2是解绑手机号<br>
	 */
	private int typeMenu = -1;

	public void changeMenu(int type) {
		if (type == -1) {
			if (typeMenu == -1) {
				typeMenu = 1;
			} else {
				return;
			}
		} else {
			if (type == typeMenu) {
				cleanText();
				return;
			} else {
				typeMenu = type;
			}
		}
		try {
			if (typeMenu == 1) {
				getBindingBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B2.png"));
				getUnbundleBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B3.png"));
				getIphoneBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B6.png"));
			} else if (typeMenu == 2) {
				getBindingBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B1.png"));
				getUnbundleBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B4.png"));
				getIphoneBtn().setIcons(CutButtonImage.cuts("skin/8100226/button/B7.png"));
			}
			if (time < -1) {
				SendMessageUntil.toServer(Agreement.getAgreement().PhoneNumberIsNoGetAgreement());
			}
			cleanText();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 清空输入框
	 */
	public void cleanText() {
		iphoneText.setText("");
		safetyText.setText("");
		verificationText.setText("");
	}

	/**
	 * 判断异动时间
	 */
	public boolean judgeTime() {
		if (numberSGBean.getNumbertime() == null) {
			return true;
		}
		String dateToStamp = dateToStamp(numberSGBean.getNumbertime());
		long parseLong = Long.parseLong(dateToStamp);
		long time = Util.getTime();
		if ((int) ((time - parseLong) / 6000 / 60) >= 24) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 将时间转换为时间戳
	 */
	public static String dateToStamp(String s) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long ts = date.getTime();
		res = String.valueOf(ts);
		return res;
	}

	/**
	 * 改变按钮形态
	 * 
	 * @param btn
	 * @param text
	 * @param path
	 * @param type
	 *            1是单张图片 3是切图
	 */
	public void changBtn(int btn, String text, String path, int type) {
		if (time >= -1) {
            return;
        }
		try {
			verificationBtn.setBtn(btn);
			verificationBtn.setText(text);
			if (type == 1) {
				verificationBtn.setIcon(CutButtonImage.getImage(path, -1, -1));
			} else if (type == 3) {
				verificationBtn.setIcons(CutButtonImage.cuts(path));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	int time = -2;

	/**
	 * 获取验证码
	 */
	public void sendIphonegetVerification() {
		if (iphoneText.getText() != null && !"".equals(iphoneText.getText())) {
			
			// countDownTime();
			iphoneNumber = iphoneText.getText();
			verification = sendsms.sendUNtil(iphoneNumber);
			if ("error".equals(verification)) {
				ZhuFrame.getZhuJpanel().addPrompt2("获取验证码失败，请查看手机是否正确");
			} else if ("logup".equals(verification)) {
				ZhuFrame.getZhuJpanel().addPrompt2("获取验证码失败，手机发送超过次数");
			} else {
				changBtn(-1, "300", "skin/8100226/button/B10.png", 1);
				time = 300;
				ZhuFrame.getZhuJpanel().addPrompt2("发送验证码成功，请及时验证");
			}
		} else {
			ZhuFrame.getZhuJpanel().addPrompt2("请先输入手机号");
		}
	}

	/** 倒计时发送按钮 */
	// public void countDownTime() {
	// new Thread(new Runnable() {
	// int time = 300;
	//
	// @Override
	// public void run() {
	// btnchange = true;
	// while (time > 0) {
	// try {
	// verificationBtn.setText((time--) + "");
	// Thread.sleep(1000);
	// } catch (InterruptedException e) {
	// e.printStackTrace();
	// }
	// }
	// btnchange = false;
	// SendMessageUntil.toServer(Agreement.getAgreement().PhoneNumberIsNoGetAgreement());
	// }
	// }).start();
	// }

	/**
	 * 判断是否绑定手机
	 */
	public void judgePhone(PhoneNumberSGBean sgBean) {
		if (sgBean == null) {
			return;
		}
		this.numberSGBean = sgBean;
		if ("nophone".equals(numberSGBean.getPhone()) && typeMenu == 2) {
			changBtn(-1, "未绑定", "skin/8100226/button/B10.png", 1);
			return;
		} else if (!"nophone".equals(numberSGBean.getPhone()) && typeMenu == 1) {
			changBtn(-1, "已绑定", "skin/8100226/button/B10.png", 1);
			return;
		} else if ("nophone".equals(numberSGBean.getPhone()) && typeMenu == 1) {
			if (!judgeTime()) {
				changBtn(-1, "无法绑定", "skin/8100226/button/B10.png", 1);
				ZhuFrame.getZhuJpanel().addPrompt2("解绑还没有过24小时，无法绑定");
				return;
			} else {
				changBtn(1, "", "skin/8100226/button/B5.png", 3);
			}
		} else {
			changBtn(1, "", "skin/8100226/button/B5.png", 3);
		}
	}

	/**
	 * 背景
	 */
	private ImageIcon iconBack;
	int in = 0;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/old/background/S2.png", -1, -1);
		}
		g.drawImage(iconBack.getImage(), 0, 0, 362, 308, this);
		if (time >= -1) {
			if (in >= 100) {
				verificationBtn.setText((time > 0 ? time : 0) + "");
				time--;
				in = 0;
				if (time < -1) {
					SendMessageUntil.toServer(Agreement.getAgreement().PhoneNumberIsNoGetAgreement());
				}
			}
			in++;
		}
	}

	public IphoneVerifyBtn getBindingBtn() {
		if (bindingBtn == null) {
			bindingBtn = new IphoneVerifyBtn("skin/8100226/button/B2.png", 1, 1, this);
			bindingBtn.setBounds(44, 21, 70, 35);
			this.add(bindingBtn);
		}
		return bindingBtn;
	}

	public void setBindingBtn(IphoneVerifyBtn bindingBtn) {
		this.bindingBtn = bindingBtn;
	}

	public IphoneVerifyBtn getUnbundleBtn() {
		if (unbundleBtn == null) {
			unbundleBtn = new IphoneVerifyBtn("skin/8100226/button/B3.png", 1, 2, this);
			unbundleBtn.setBounds(44 + 72, 21, 70, 35);
			this.add(unbundleBtn);
		}
		return unbundleBtn;
	}

	public void setUnbundleBtn(IphoneVerifyBtn unbundleBtn) {
		this.unbundleBtn = unbundleBtn;
	}

	public IphoneVerifyBtn getIphoneBtn() {
		if (iphoneBtn == null) {
			iphoneBtn = new IphoneVerifyBtn("skin/8100226/button/B6.png", 1, 3, this);
			iphoneBtn.setBounds(123, 170, 140, 26);
			this.add(iphoneBtn);
		}
		return iphoneBtn;
	}

	public void setIphoneBtn(IphoneVerifyBtn iphoneBtn) {
		this.iphoneBtn = iphoneBtn;
	}

	public IphoneVerifyBtn getVerificationBtn() {
		if (verificationBtn == null) {
			verificationBtn = new IphoneVerifyBtn("skin/8100226/button/B5.png", 1, 4, this);
			verificationBtn.setBounds(248, 70, 68, 17);
			this.add(verificationBtn);
		}
		return verificationBtn;
	}

	public void setVerificationBtn(IphoneVerifyBtn verificationBtn) {
		this.verificationBtn = verificationBtn;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public JTextField getIphoneText() {
		if (iphoneText == null) {
			iphoneText = new JTextField();
			iphoneText.setBorder(null);
			iphoneText.setOpaque(false);
			iphoneText.setBounds(134, 71, 108, 16);
			iphoneText.setForeground(Color.white);
			iphoneText.setCaretColor(Color.white);
			iphoneText.addKeyListener(new KeyListener() {
				// 键盘监听
				@Override
				public void keyTyped(KeyEvent e) {
					int keyChar = e.getKeyChar();
					// 让该输入框只能输入数字
					if (keyChar < KeyEvent.VK_0 || keyChar > KeyEvent.VK_9) {
						e.consume();
						return;
					}
					String key = iphoneText.getText();
					if (key.length() >= 11) {
						e.consume();
						return;
					}
				}

				@Override
				public void keyReleased(KeyEvent e) {
				}

				@Override
				public void keyPressed(KeyEvent e) {

				}
			});
			this.add(iphoneText);
		}
		return iphoneText;
	}

	public void setIphoneText(JTextField iphoneText) {
		this.iphoneText = iphoneText;
	}

	public JTextField getVerificationText() {
		if (verificationText == null) {
			verificationText = new JTextField();
			verificationText.setBorder(null);
			verificationText.setOpaque(false);
			verificationText.setBounds(134, 104, 181, 16);
			verificationText.setForeground(Color.white);
			verificationText.setCaretColor(Color.white);
			this.add(verificationText);
		}
		return verificationText;
	}

	public void setVerificationText(JTextField verificationText) {
		this.verificationText = verificationText;
	}

	public JTextField getSafetyText() {
		if (safetyText == null) {
			safetyText = new JTextField();
			safetyText.setBorder(null);
			safetyText.setOpaque(false);
			safetyText.setBounds(134, 138, 181, 16);
			safetyText.setForeground(Color.white);
			safetyText.setCaretColor(Color.white);
			this.add(safetyText);
		}
		return safetyText;
	}

	public void setSafetyText(JTextField safetyText) {
		this.safetyText = safetyText;
	}

	public int getTypeMenu() {
		return typeMenu;
	}

	public void setTypeMenu(int typeMenu) {
		this.typeMenu = typeMenu;
	}

	public String getVerification() {
		return verification;
	}

	public void setVerification(String verification) {
		this.verification = verification;
	}

	public String getIphoneNumber() {
		return iphoneNumber;
	}

	public void setIphoneNumber(String iphoneNumber) {
		this.iphoneNumber = iphoneNumber;
	}

	public PhoneNumberSGBean getNumberSGBean() {
		return numberSGBean;
	}

	public void setNumberSGBean(PhoneNumberSGBean numberSGBean) {
		this.numberSGBean = numberSGBean;
	}

}
